package com.wing.cabinet.controller.app;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.cabinet.model.entity.AboveTheQuotaApply;
import com.wing.cabinet.model.request.AboveTheQuotaApplyForm;
import com.wing.cabinet.model.response.AboveTheQuotaApplyVO;
import com.wing.cabinet.service.AboveTheQuotaApplyService;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.web.utils.JwtUtils;
import com.wing.web.utils.LocaleMessage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author
 */
@Slf4j
@Validated
@Api(value = "App-超额申请表管理", tags = "超额申请表管理")
@RestController("appAboveTheQuotaApplyController")
@RequestMapping("cabinet/app/aboveTheQuotaApply")
public class AboveTheQuotaApplyController {

    @Autowired
    private AboveTheQuotaApplyService aboveTheQuotaApplyService;
    @Autowired
    private LocaleMessage localeMessage;

    @ApiOperation(value = "查询所有超额申请表", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
    })
    @GetMapping("/list")
    public PageResult<AboveTheQuotaApplyVO> list(
            @RequestParam( defaultValue = "1", required = false) Integer page
            ,@RequestParam(defaultValue = "10", required = false) Integer limit
            ,@RequestParam(defaultValue = "create_time", required = false) String sort
            ,@RequestParam( defaultValue = "desc", required = false) String order
            ,AboveTheQuotaApply.CheckStatus checkStatus
    ) {
        Page<AboveTheQuotaApplyVO> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", sort);
            params.put("order", order);
        }
        if (checkStatus!=null){
            params.put("checkStatus",checkStatus.getValue());
        }
        params.put("user_id", JwtUtils.getLoginUserId());
        Page<AboveTheQuotaApplyVO> list = aboveTheQuotaApplyService.pageList4app(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @GetMapping("/{id}")
    public JsonResult detail(@PathVariable("id") Long aboveTheQuotaApplyId) {
        AboveTheQuotaApplyVO aboveTheQuotaApplyVO = aboveTheQuotaApplyService.get4appById(aboveTheQuotaApplyId);
        if (aboveTheQuotaApplyVO == null) {
            return JsonResult.fail(localeMessage.getMessage("common.fail"));
        }
        return JsonResult.success(localeMessage.getMessage("common.success"), aboveTheQuotaApplyVO);
    }
    @ApiOperation(value = "添加超额领新申请表", notes = "添加")
    @PostMapping("/submitAboveTheQuotaApplyForm")
    public JsonResult submitAboveTheQuotaApplyForm(ServletRequest servletRequest, @RequestBody AboveTheQuotaApplyForm aboveTheQuotaApply) {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        String domain = request.getHeader("domain");
        log.info("submitAboveTheQuotaApplyForm out param:{}", JSON.toJSONString(aboveTheQuotaApply));
        try {
            aboveTheQuotaApplyService.submitAboveTheQuotaApplyForm(aboveTheQuotaApply, domain);

            return JsonResult.success("操作成功");
        }catch (Exception e){
            log.error("添加领新申请接口异常，原因是：{} ", e.getMessage(), e);
            return JsonResult.fail("操作失败:" + e.getMessage());
        }
    }
    @PostMapping()
    public JsonResult add(@Validated AboveTheQuotaApplyForm aboveTheQuotaApplyForm) {
        if (aboveTheQuotaApplyService.add(aboveTheQuotaApplyForm) > 0) {
            return JsonResult.success(localeMessage.getMessage("common.success"));
        }
        return JsonResult.fail(localeMessage.getMessage("common.fail"));
    }
    @ApiOperation(value = "查询超额领新单信息和产品信息", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "aboveTheQuotaApplyId", value = "领新申请id", required = true, dataType = "Integer", paramType = "path")
    })
    @GetMapping("/getAboveTheQuotaApplyVOInfo")
    public JsonResult<AboveTheQuotaApplyVO> getWorkOrderInfo(@RequestParam("aboveTheQuotaApplyId") Long aboveTheQuotaApplyId){
        AboveTheQuotaApplyVO aboveTheQuotaApplyVO = aboveTheQuotaApplyService.getAboveTheQuotaApplyVOInfo(aboveTheQuotaApplyId);
        return JsonResult.success(aboveTheQuotaApplyVO);

    }
}
